package cn.com.websocket.service.impl;


import cn.com.websocket.handle.SocketHandle;
import cn.com.websocket.po.Message;
import cn.com.websocket.util.StringUtil;
import com.alibaba.fastjson.JSONObject;

import javax.websocket.Session;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public abstract class AbsMessageStrategy {

    /**
     * <p>处理消息</p>
     * @param session 当前登陆用户
     * @param message
     * */
    public abstract void handleMessage(Session session, Message message);

    /**
     * <p>发送消息</p>
     * @param session
     * @param message
     * @return
     * */
    protected boolean sendMessage(Session session, Message message) {
        boolean isSend = Boolean.FALSE;
        if(session != null){
            String sendMsg = JSONObject.toJSONString(message);
            try {
                synchronized (session) {
                    session.getBasicRemote().sendText(sendMsg);
                    isSend=Boolean.TRUE;
                }
            }catch ( Exception e){
                e.printStackTrace();
                isSend=Boolean.FALSE;
            }
        }
        return isSend;
    }

    /**
     * <p>查询双方历史记录且进行合并后进行排序</p>
     * @param source
     * @param target
     * @return
     * */
    protected List<Message> getCacheHistory(String source, String target){
        List<Message> messageList1 = SocketHandle.historyMessage.get(SocketHandle.getHistorySourceWithTargetKey(source,target));
        List<Message> messageList2 = SocketHandle.historyMessage.get(SocketHandle.getHistorySourceWithTargetKey(target,source));
        return mergeAndSort(messageList1,messageList2);
    }

    /**
     * <p>合并并且进行按时间和id进行排序</p>
     * @param messageList
     * @return
     * */
    protected List<Message> mergeAndSort(List<Message> ...messageList){
        if(null==messageList||messageList.length==0){return null;}
        List<Message> messages = new ArrayList<>();
        for(List<Message> msgList:messageList){
            if(StringUtil.isEmpty(msgList)){continue;}
            messages.addAll(msgList);
        }
        if(!messages.isEmpty()){
            messages=messages.stream().sorted(Comparator.comparing(Message::getCreateTime).thenComparing(Message::getId)).collect(Collectors.toList());
        }
        return messages;
    }
    /**
     * <p>把消息丢至历史记录中</p>
     * */
    public void putHistoryMessage(Message message){
        String key = SocketHandle.getHistorySourceWithTargetKey(message.getSource(),message.getTarget());
        List<Message> sourceList = SocketHandle.historyMessage.get(key);
        if(null==sourceList){
            sourceList = new ArrayList<>();
        }
        sourceList.add(message);
        SocketHandle.historyMessage.put(key,sourceList);
    }

    /**
     * <p>保存双方的历史记录至缓存</p>
     * @param message
     * @param sourceList
     * @param targetList
     * */
    protected void putCacheHistory(Message message,List<Message> sourceList,List<Message> targetList){
        String sourceKey = SocketHandle.getHistorySourceWithTargetKey(message.getSource(),message.getTarget());
        String targetKey = SocketHandle.getHistorySourceWithTargetKey(message.getTarget(),message.getSource());
        SocketHandle.historyMessage.put(sourceKey,sourceList);
        SocketHandle.historyMessage.put(targetKey,targetList);
    }

}
